﻿using Furion.DependencyInjection;
using Furion.Schedule;
using Microsoft.Extensions.Logging;
using OnWork.DataCenter.BLL.DataCenter;

namespace OnWork.DataCenter.BLL.TaskScheduler
{
    /// <summary>
    /// 定时任务BLL
    /// </summary>
    public class TaskSchedulerBLL : IScoped
    {
        private static ILogger<TaskSchedulerBLL> _logger;

        /// <summary>
        /// 定时任务类
        /// </summary>
        private readonly ISchedulerFactory _schedulerFactory;

        /// <summary>
        /// 数据库查询对象
        /// </summary>
        private readonly DSTaskBLL _dSTaskBLL;

        public TaskSchedulerBLL(ISchedulerFactory schedulerFactory, DSTaskBLL dSTaskBLL, ILogger<TaskSchedulerBLL> logger)
        {
            _schedulerFactory = schedulerFactory;
            _dSTaskBLL = dSTaskBLL;
            _logger = logger;
        }

        /// <summary>
        /// 第一次启动时初始化任务
        /// </summary>
        /// <returns></returns>
        public async Task InitJobs()
        {
            //如果没有则更新任务
            var count = _schedulerFactory.GetJobs().Count();
            if (count == 0)
            {
                _logger.LogInformation("初始化调度定时任务!");
                await UpdateJobs();
            }
        }

        /// <summary>
        /// 更新全部任务
        /// </summary>
        /// <returns></returns>
        public async Task UpdateJobs()
        {
            _schedulerFactory.RemoveAll();
            var jobs = await _dSTaskBLL.GetProcessTypeListAsync(); //获取配置的任务
            if (jobs != null)
            {
                foreach (var item in jobs)
                {
                    TriggerBuilder triggerBuilder = null;

                    switch (item.PeriodType)
                    {
                        case DAL.DataCenter.DSTaskTimeType.Period:
                            triggerBuilder = Triggers.Period(int.Parse(item.PeriodConfig));
                            break;

                        case DAL.DataCenter.DSTaskTimeType.PeriodSeconds:
                            triggerBuilder = Triggers.PeriodSeconds(int.Parse(item.PeriodConfig));
                            break;

                        case DAL.DataCenter.DSTaskTimeType.PeriodMinutes:
                            triggerBuilder = Triggers.PeriodMinutes(int.Parse(item.PeriodConfig));
                            break;

                        case DAL.DataCenter.DSTaskTimeType.PeriodHours:
                            triggerBuilder = Triggers.PeriodHours(int.Parse(item.PeriodConfig));
                            break;

                        case DAL.DataCenter.DSTaskTimeType.Cron:
                            triggerBuilder = Triggers.Cron(item.PeriodConfig);
                            break;

                        default:
                            break;
                    }
                    _schedulerFactory.AddJob<ProcessJob>(item.ID.ToString(), triggerBuilder);
                }
                _logger.LogInformation("更新加载调度定时任务!");
            }
        }

        public void AddJob()
        {
            _schedulerFactory.AddJob<ProcessJob>("动态作业 Id", Triggers.Secondly());
        }
    }
}